package com.jzo2o.customer.controller.consumer;


import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/consumer/address-book")
public class AddressController {

    @Autowired
    private IAddressBookService addressBookService;

    @PostMapping
    public void add(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.add(dto);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO dto) {

        addressBookService.update(id, dto);
    }


    @GetMapping("/{id}")
    public AddressBookResDTO detail(@PathVariable("id") Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtil.toBean(addressBook, AddressBookResDTO.class);
    }

    @PutMapping("/default")
    public void updateDefaultStatus(@RequestParam("id") Long id, @RequestParam("flag") Integer flag) {
        Long userId = UserContext.currentUserId();
        addressBookService.updateDefaultStatus(userId, id, flag);
    }

    @DeleteMapping("/batch")
    public void logicallyDelete(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }

    @GetMapping("/page")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO dto) {
        return addressBookService.page(dto);
    }

    @GetMapping("/defaultAddress")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }
}
